/** 
 * Project Name:sys-portal-Extjs 
 * File Name:MainCtrl.js 
 * Package Name: 
 * Author : 吴Yuan|小森森
 * Date:2018年4月13日下午4:50:35 
 * Copyright (c) 2018, 15909910367@163.com All Rights Reserved. 
 * 
 */
Ext.define('Jason.view.protal.module.systemManager.right.organize.MainCtrl', {
    extend: 'Wys.basic.BaseCtrl',
    alias: 'controller.rightOrganizeCtrl',
    init:function(){
        var me = this;
            me.getView().down('#areaLeftView').getStore().load();
    },
    formItems: function (parentCode) {
        return [{
            xtype: 'fieldset',
            title: '基本信息',
            collapsible: false,
            defaultType: 'textfield',
            layout: {
                type: 'table',
                columns: 2
            },
            defaults: {
                width: '100%',
                labelWidth: 80
            },
            items: [
                { fieldLabel: 'id', name: 'id', xtype: 'textfield', value: '', hidden: true },
                { xtype: 'hiddenfield', name: 'deptsuper', value: parentCode },
                { fieldLabel: '机构名称', name: 'deptname', xtype: 'textfield', allowBlank: false, afterLabelTextTpl: cfg.required },
                { fieldLabel: '机构编码', name: 'deptCode', xytpe: 'textfield',allowBlank:false , emptyText: '请输入组织机构代码/统一社会信用代码' },
                { fieldLabel: '行政区划', name: 'areaCode', 
                    xtype: 'comboTree',
                    valueField: 'code',
                    config:{
                        valueField: 'code',
                    },
                    displayField: 'label', 
                    store: Ext.create('Ext.data.TreeStore', {
                        fields: [],
                        autoLoad: true,
                        rootVisible: true,
                        proxy: {
                            type: 'ajax',
                            url: reqUrl.sys.area.findTreeDate,
                            reader : {
                                rootProperty    : 'data',
                                totalProperty   : 'total',
                                successProperty : 'success'
                            }
                        }
                    }),
                    allowBlank: false,
                    afterLabelTextTpl: cfg.required,
                    emptyText: '请选择...',
                },
                {
                    xtype: 'RemoteCombobox',
                    name: 'depttype',
                    emptyText: '请选择...',
                    model: Ext.create('Ext.data.Model', {
                        fields: ['key', 'value']
                    }),
                    valueField: 'value',
                    displayField: 'key',
                    proxy_url: reqUrl.sys.comboData.dict + "DEPTTYPE",
                    allowBlank: false,
                    afterLabelTextTpl: cfg.required,
                    blankText: '机构类型不能为空',
                    fieldLabel: '组织类型',
                    value: 1
                },
                { fieldLabel: '机构联系电话',name:'deptTel',vtype:'checkMobildAndTel'},
                { fieldLabel: '电子邮件',name:'deptEmail',vtype:'checkEmail'},
                { fieldLabel: '机构传真',name:'deptFax',vtype:'checkFax'},
                { fieldLabel: '邮政编码',name:'deptPost'},
                { fieldLabel: '负责人/法人',name:'deptCommander',vtype:'checkName'},
                { fieldLabel: '负责人/法人电话',name:'deptCommanderPhone',vtype:'checkMobile'},
                { fieldLabel: '网址',name:'deptDomainSite',vtype:'checkWebSite'},
                { fieldLabel: '注册地址',name:'deptAddress',vtype:'checkAddress'},
                { fieldLabel: '备注', name: 'deptremarks', flex: 1, xtype: 'textareafield', grow: true ,colspan:2}
            ]
        }];
    },
    onNewButtonClick: function (btn) {
        var me = this;
        Ext.create('Wys.form.FormWindow', {
            width: 700,
            height: Ext.getBody().getHeight() * 0.8,
            title: '组织机构',
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemManager.right.organize.OrganizeModel',
                gridPanel: me.getView().down('#orgsView'),
                items: me.formItems(-1)
            }
        }).show();
    },
    onNewWithCopyButtonClick: function (btn) {
        var me = this;
        Ext.create('Wys.form.FormWindow', {
            width: 700,
            height: Ext.getBody().getHeight() * 0.8,
            title: '组织机构',
            model: '',
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemManager.right.organize.OrganizeTreeModel',
                items: me.formItems()
            }
        }).show();
    },
    onEditButtonClick: function (btn, record) {
        var me = this;
        grid = this.getView().down('#orgsView');
        Ext.create('Wys.form.FormWindow', {
            width: 700,
            height: Ext.getBody().getHeight() * 0.8,
            title: ' 『' + record.get('deptname') + '』部门 信息',
            listeners:{
                show:function(win){
                    var editForm = win.down('form');
                    editForm.findField('deptCode').setReadOnly(true);
                    editForm.findField('deptCode').setDisabled(true);
                }
            },
            config: {
                model: 'Jason.model.protal.module.systemManager.right.organize.OrganizeTreeModel',
                operatetype: 'edit',
                gridPanel: grid,
                items: me.formItems(),
                formData: record
            }
        }).show();
    },
    onDeleteButtonClick: function (btn) {
        var grid = this.getView().down('#orgsView'),
            gridStore = grid.getStore(),
            selectGridData = grid.getSelection(),
            removePK = [],
            removeName = [];
        if (selectGridData.length === 0) {
            EUtil.toastWarn('请选择要操作的数据');
            return;
        }
        Ext.Array.each(selectGridData, function (item) {
            removeName.push(item.data.deptname);
            removePK.push(item.data.id);
        });
        EUtil.showMsg({
            option: 3,
            title: '温馨提示',
            message: '您确定删除<span style="color:red;">『' + removeName.join() + '』</span>吗？',
            callback: function (btn) {
                if (btn === 'ok') {
                    gridStore.remove(selectGridData);
                    gridStore.sync({
                        callback: function () {
                            gridStore.reload();
                        }
                    });
                }
            }
        });
    },

    refreshLeftMenuTree: function (btn) {
        var treeStore = btn.up('#orgsView').getStore();
        treeStore.load();
    },

    /** 新版  **/
    onGridCommand: function (item, command, record, recordIndex, cellIndex) {
        var me = this;
        if (command === 'modify') {/** 修改 */
            me.onEditButtonClick(null, record);
        } else if (command === 'add') {
            Ext.create('Wys.form.FormWindow', {
                width: 700,
                height: Ext.getBody().getHeight() * 0.8,
                title: '组织机构',
                config: {
                    operatetype: 'new',
                    model: 'Jason.model.protal.module.systemManager.right.organize.OrganizeModel',
                    gridPanel: me.getView().down('#orgsView'),
                    items: me.formItems(record.get('id'))
                }
            }).show();
        } else if (command === 'remove') {
            EUtil.showMsg({
                option: 3,
                title: '温馨提示',
                message: '您确定删除<span style="color:red;">『' + record.get('deptname') + '』</span>吗,？',
                callback: function (btn) {
                    if (btn === 'ok') {
                        EUtil.RS({
                            url: item.gridRef.getStore().getProxy().api.destroy,
                            params: { ids: record.get('id') },
                            callback: function (resp) {
                                if (resp.success) {
                                    EUtil.showSuccess(Ext.String.format(I18N.DeleteMessage[0],
                                        record.get('deptname')
                                    ));
                                    item.gridRef.getStore().reload();
                                } else {
                                    EUtil.showError(
                                        Ext.String.format(I18N.FailedOtherCode,
                                            resp.code,
                                            resp.msg)
                                    );
                                }
                            }
                        })
                    }
                }
            });
        }
    },
    onTreeNodeClick:function(node, e){
        var me = this,
            orgView = me.getView().down('#orgsView');
        orgView.load({
            areaCode: e.get('code')
        }); 
    }
});